var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num:0,
    total:0
  },

  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          windowHeight: res.windowHeight,
          id: options.id,
        })
      }
    })
    this.getCardDetail();
  },
  onShow: function () {
  
  },
  getCardDetail:function(){
    var that =this;
    var id = this.data.id;
    var params = new Object()
    params.id = this.data.id;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'coupons/info',
      success: function (res) {
        wx.hideLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            cardDetail:res.data.data,
            stock: res.data.data.stock,
            price: res.data.data.price
          })
        }

      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  addCard:function(){
    var  num = this.data.num/1;
    var stock = this.data.stock/1;
    var price = this.data.price/1
    console.log(price);
    if (num<stock){
      num++
    }else{
      util.showError("库存不足");
      return false
    }

    this.setData({
      num: num,
      total: (num * price).toFixed(2)
    })

  },
  minusCard: function () {
    var num = this.data.num / 1;
    var stock = this.data.stock / 1;
    var price = this.data.price / 1;
    if (num >=1) {
      num--
    } else {
      return false
    }

    this.setData({
      num: num,
      total: (num * price).toFixed(2)
    })

  },
  pay:function(){
    var that=this;
    var num = this.data.num;
    var id = this.data.id;
    var record = { 'coupons_id': id, 'num':num};
    if(num<=0){
      return false;
    }
    var params = []
    params.push(record);
    params = JSON.stringify(params)
    api.POST({
      params: {'record':params},
      url: app.globalData.base_url + 'couponsorder/submit',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.wxPayOrder(res.data.data.result.oid);
        }

      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  //支付接口
  wxPayOrder: function (oid) {
    var params = new Object()
    var that = this;
    params.oid = oid;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'payment/getCouponsWechatInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {


          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              wx.redirectTo({
                url: '../cardMy/cardMy'
              })
            },
            fail: function (error) {
              util.showError("支付失败");
              // wx.navigateTo({
              //   url: '../cardMy/cardMy'
              // })
            }
          })
        } else {
          util.showError(res.data.message);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  }
})